package com.ruoyi.web.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Arrays;
import java.util.Map;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.system.service.ISysMenuService;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.SysBusinessModuleVo;
import com.ruoyi.system.domain.bo.SysBusinessModuleBo;
import com.ruoyi.system.service.ISysBusinessModuleService;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;

/**
 * 租户模块Controller
 *
 * @author pengju.liao
 * @date 2022-03-21
 */
@Validated
@Api(value = "租户模块控制器", tags = {"租户模块管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/businessModule")
public class SysBusinessModuleController extends BaseController {

    private final ISysBusinessModuleService iSysBusinessModuleService;

    private final ISysMenuService menuService;

    /**
     * 查询租户模块列表
     */
    @ApiOperation("查询租户模块列表")
    @SaCheckPermission("system:businessModule:list")
    @GetMapping("/list")
    public TableDataInfo<SysBusinessModuleVo> list(@Validated(QueryGroup.class) SysBusinessModuleBo bo, PageQuery pageQuery) {
        return iSysBusinessModuleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出租户模块列表
     */
    @ApiOperation("导出租户模块列表")
    @SaCheckPermission("system:businessModule:export")
    @Log(title = "租户模块", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated SysBusinessModuleBo bo, HttpServletResponse response) {
        List<SysBusinessModuleVo> list = iSysBusinessModuleService.queryList(bo);
        ExcelUtil.exportExcel(list, "租户模块", SysBusinessModuleVo.class, response);
    }

    /**
     * 获取租户模块详细信息
     */
    @ApiOperation("获取租户模块详细信息")
    @SaCheckPermission("system:businessModule:query")
    @GetMapping("/{id}")
    public R<Map<String, Object>> getInfo(@ApiParam("主键")
                                          @NotNull(message = "主键不能为空")
                                          @PathVariable("id") Long id) {
        Map<String, Object> map = new HashMap<>();
        map.put("sysBusinessModuleVo", iSysBusinessModuleService.queryById(id));
        List<SysMenu> menus = menuService.selectBaseMenuList();
        map.put("menus", menuService.buildMenuTreeSelect(menus));
        map.put("checkedKeys", iSysBusinessModuleService.selectMenuListByModuleId(id));
        return R.ok(map);
    }


    /**
     * 新增租户模块
     */
    @ApiOperation("新增租户模块")
    @SaCheckPermission("system:businessModule:add")
    @Log(title = "租户模块", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysBusinessModuleBo bo) {
        // TODO 需要先判断租户模块名称是否存在
        return toAjax(iSysBusinessModuleService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改租户模块
     */
    @ApiOperation("修改租户模块")
    @SaCheckPermission("system:businessModule:edit")
    @Log(title = "租户模块", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysBusinessModuleBo bo) {
        return toAjax(iSysBusinessModuleService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除租户模块
     */
    @ApiOperation("删除租户模块")
    @SaCheckPermission("system:businessModule:remove")
    @Log(title = "租户模块", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iSysBusinessModuleService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
